package com.wuyu.acl.controller;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wuyu.acl.common.Result;
import com.wuyu.acl.dto.RolePageQueryDto;
import com.wuyu.acl.po.AdminRole;
import com.wuyu.acl.po.Role;
import com.wuyu.acl.po.RolePermission;
import com.wuyu.acl.service.AdminRoleService;
import com.wuyu.acl.service.RolePermissionService;
import com.wuyu.acl.service.RoleService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Tag(name = "角色管理")
@RestController
@RequestMapping("/admin/acl/role")
public class RoleController {

    @Resource
    private RoleService roleService;

    @Resource
    private AdminRoleService adminRoleService;

    @Resource
    private RolePermissionService rolePermissionService;

    /**
     * 角色管理：查询所有的角色
     *
     * @return
     */
    @Operation(summary = "查询所有的角色")
    @GetMapping
    public Result getAllRoles() {
        List<Role> roleList = roleService.list();
        return Result.ok(roleList);
    }

    /**
     * 角色管理：添加角色
     *
     * @param role
     * @return
     */
    @Operation(summary = "添加角色")
    @PostMapping("/admin/acl/role")
    public Result add(@RequestBody Role role) {
        roleService.save(role);
        return Result.ok();
    }


    /**
     * 角色管理：删除角色
     *
     * @param ids
     * @return
     */
    @Operation(summary = "删除角色")
    @Transactional
    @DeleteMapping
    public Result remove(@RequestBody List<Long> ids) {
        // 1. 删除角色表中的信息
        roleService.removeBatchByIds(ids);
        // 2. 删除用户角色表中的信息
        adminRoleService.remove(new LambdaQueryWrapper<AdminRole>()
                .in(AdminRole::getRoleId, ids));
        // 3. 删除角色权限表中的信息
        rolePermissionService.remove(new LambdaQueryWrapper<RolePermission>()
                .in(RolePermission::getRoleId, ids));
        return Result.ok();
    }

    /**
     * 角色管理：修改角色
     *
     * @param role
     * @return
     */
    @Operation(summary = "修改角色")
    @PutMapping
    public Result update(@RequestBody Role role) {
        roleService.updateById(role);
        return Result.ok();
    }

    /**
     * 角色管理：条件分页查询
     *
     * @param page
     * @param size
     * @param rolePageQueryDto
     * @return
     */
    @Operation(summary = "条件分页查询")
    @GetMapping("/{page}/{size}")
    public Result page(@PathVariable("page") Integer page,
                       @PathVariable("size") Integer size,
                       RolePageQueryDto rolePageQueryDto) {
        String keyWord = rolePageQueryDto.getKeyWord();
        Page<Role> rolePage = roleService.page(new Page<>(page, size), new LambdaQueryWrapper<Role>()
                .like(StrUtil.isNotBlank(keyWord), Role::getName, keyWord)
                .or()
                .like(StrUtil.isNotBlank(keyWord), Role::getCode, keyWord));
        return Result.ok(rolePage);
    }

}
